package cn.me.controller;

import cn.me.pojo.Payrank;
import cn.me.pojo.Section;
import cn.me.pojo.User;
import cn.me.service.PayRankService;
import cn.me.service.SectionService;
import cn.me.service.UserService;
import cn.me.service.impl.SectionServiceImpl;
import cn.me.service.impl.UserServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private SectionService sectionService;
    @Autowired
    private PayRankService payRankService;
    /*动态分页查询*/
    @RequestMapping("all")
    public String all(Model model,
           @RequestParam(value = "tid",defaultValue = "0")Integer tid,
           @RequestParam(value = "name",defaultValue = "")String name,
           @RequestParam(value = "page",defaultValue = "1")Integer page,
           @RequestParam(value = "size",defaultValue = "3")Integer size,
           @RequestParam(value = "uout",defaultValue = "2")Integer uout
                      ){
        //调用user的动态分页查询接口
        PageInfo<User> pageInfo = userService.all(uout,name, tid, page, size);
        //调用section的全查接口
        List<Section> sections = sectionService.selectAll();

        //返回结果
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("sections",sections);
        model.addAttribute("tid",tid);
        model.addAttribute("name",name);
        model.addAttribute("uout",uout);
        return "userList";
    }

    //新键员工的页面
    @RequestMapping("/add")
    public String add(Model model){
        List<Payrank> payranks = payRankService.all();
        List<Section> sections = sectionService.selectAll();
        model.addAttribute("payranks",payranks);
        model.addAttribute("sections",sections);
        return "userAdd";
    }

    //添加
    @RequestMapping("/toAdd")
    public String toadd(User user){
        try {
            userService.add(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //重定向到全查
        return "redirect:/user/all";
    }

    //修改页面
    @RequestMapping("/update/{uid}")
    public String update(@PathVariable int uid,Model model){
        List<Payrank> payranks = payRankService.all();
        List<Section> sections = sectionService.selectAll();
        User user = userService.selectByUid(uid);
        model.addAttribute("payranks",payranks);
        model.addAttribute("sections",sections);
        model.addAttribute("user",user);
        return "userUpdate";
    }

    //去修改
    @RequestMapping("/toUpdate")
    public String toUpdate(User user){
        userService.toUpdate(user);
        return "redirect:/user/all";

    }

    //去删除
    @RequestMapping("/deletes")
    @ResponseBody
    public void del(String array){
        System.out.println(array);
        String[] split = array.split(",");
        userService.delete(split);

    }
}
